#pragma once

/*
 * global main service class.
 */
#include <map>
#include "anet.hpp"
#include "asio.hpp"
#include "asio/detail/noncopyable.hpp"

namespace anet {
	namespace tcp {
		// CMainService is main service, it is a global instance to run in a unique thread.
        class CMainService final : asio::noncopyable {
		protected:
			CMainService();
			virtual ~CMainService();	
			typedef std::unique_ptr<asio::io_context> ioContextPtr;
			typedef std::unique_ptr<asio::io_context::work> workPtr;

		public:
			// global instance
		    static CMainService& instance();

			// post function to asio service to execute.
			void post(std::function<void()> func);

			// run asio service with counters where busy return true, else return false.
			bool run(int count);

		private:
			// io_context and its worker.
			ioContextPtr m_service{ nullptr };
			workPtr m_work{ nullptr };
		};
   }
}
